package org.jeecg.modules.param.sysparam.controller;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.sysparam.entity.BikeSysParam;
import org.jeecg.modules.param.sysparam.service.IBikeSysParamService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.utils.BikeResult;
import org.jeecg.modules.utils.EstUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 参数设置
 * @Author: 月月鸟
 * @Date:   2021-05-18
 * @Version: V1.0
 */
@Api(tags="参数设置")
@RestController
@RequestMapping("/sysparam/bikeSysParam")
@Slf4j
public class BikeSysParamController extends JeecgController<BikeSysParam, IBikeSysParamService> {
	@Autowired
	private IBikeSysParamService bikeSysParamService;



	/**
	 * 分页列表查询
	 *
	 * @param bikeSysParam
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="参数设置-分页列表查询", notes="参数设置-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BikeSysParam bikeSysParam,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		bikeSysParam.setUserId(BikeConstant.param_sys_user);
		QueryWrapper<BikeSysParam> queryWrapper = QueryGenerator.initQueryWrapper(bikeSysParam, req.getParameterMap());
		Page<BikeSysParam> page = new Page<BikeSysParam>(pageNo, pageSize);
		IPage<BikeSysParam> pageList = bikeSysParamService.page(page, queryWrapper);
		List<BikeSysParam> records = pageList.getRecords();
		records.sort(new Comparator<BikeSysParam>() {
			@Override
			public int compare(BikeSysParam o1, BikeSysParam o2) {
				return o1.getPName().compareTo(o2.getPName());
			}
		});
		return Result.OK(pageList);
	}

	 /**
	  * 查询参数
	  * @return
	  */
	 @ApiOperation(value="查询参数", notes="查询参数")
	 @GetMapping(value = "/getList")
	 public Result<?> getList() {
		 List<BikeSysParam> list = bikeSysParamService.getList();
		 return Result.OK(list);
	 }

	 /**
	  * 分页列表查询
	  *
	  * @param name

	  * @return
	  */
	 @ApiOperation(value="app查询系统参数", notes="app查询系统参数")
	 @GetMapping(value = "/appList")
	 public Result<?> appList(@RequestParam(value = "name",required = false) String name) {
		 QueryWrapper<BikeSysParam> bikeSysParamQueryWrapper = new QueryWrapper<>();
		 bikeSysParamQueryWrapper.eq("user_id", BikeConstant.param_sys_user);
		 if (StringUtils.isNotEmpty(name)){
			 bikeSysParamQueryWrapper.eq("p_name",name);
		 }
		 List<BikeSysParam> list = bikeSysParamService.list();
		 return Result.OK(list);
	 }
	 /**
	  * 根据参数名查询参数
	  * @return
	  */
	 @ApiOperation(value="根据参数名查询参数", notes="根据参数名查询参数")
	 @GetMapping(value = "/getParameter")
	 public Result<?> getParameterByName(@RequestParam(name = "name",required = true) String name) {
	 	return bikeSysParamService.getParameterByName(name);
	 }

	/**
	 *   添加
	 *
	 * @param bikeSysParam
	 * @return
	 */
	@AutoLog(value = "参数设置-添加")
	@ApiOperation(value="参数设置-添加", notes="参数设置-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BikeSysParam bikeSysParam) {
		bikeSysParamService.save(bikeSysParam);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param bikeSysParam
	 * @return
	 */
	@AutoLog(value = "参数设置-编辑",operateType = 3)
	@ApiOperation(value="参数设置-编辑", notes="参数设置-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BikeSysParam bikeSysParam) {
		bikeSysParamService.updateById(bikeSysParam);
		return Result.OK("编辑成功!");
	}

	 /**
	  *  编辑或新增个性预警参数
	  *
	  * @param bikeSysParam
	  * @return
	  */
	 @AutoLog(value = "报警参数-新增个性预警参数",operateType = 2)
	 @ApiOperation(value="编辑或新增个性预警参数", notes="编辑或新增个性预警参数")
	 @PutMapping(value = "/editOrAdd")
	 public Result<?> editOrAdd(@RequestBody  List<BikeSysParam> bikeSysParam) {
		 return bikeSysParamService.editOrAdd(bikeSysParam);
	 }


	 @AutoLog(value = "更改参数",operateType = 3)
	 @ApiOperation(value="更改参数", notes="更改参数")
	 @PutMapping(value = "/changeParam")
	 public Result<?> changeParam(@RequestBody List<BikeSysParam> bikeSysParam) {

		 bikeSysParamService.updateByIds(bikeSysParam);
		 return Result.OK("编辑成功!");
	 }

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "参数设置-通过id删除")
	@ApiOperation(value="参数设置-通过id删除", notes="参数设置-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {

		bikeSysParamService.removeById(id);
		return Result.OK("删除成功!");
	}

	 /**
	  *   报警参数重置
	  *
	  */
	 @AutoLog(value = "报警参数-重置",operateType = 3)
	 @ApiOperation(value="报警参数重置", notes="报警参数重置")
	 @DeleteMapping(value = "/reset")
	 public Result<?> reset() {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 QueryWrapper<BikeSysParam> wrapper = new QueryWrapper<>();
		 wrapper.eq("user_id",sysUser.getUsername());
		 List<BikeSysParam> list = bikeSysParamService.list(wrapper);
		 if (list.size()!=0){
			 bikeSysParamService.remove(wrapper);
		 }
		 return Result.OK("删除成功!");
	 }
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "参数设置-批量删除")
	@ApiOperation(value="参数设置-批量删除", notes="参数设置-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.bikeSysParamService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(value="参数设置-通过id查询", notes="参数设置-通过id查询")
	@GetMapping(value = "/queryById")
	public BikeResult queryById() {

		BikeSysParam bikeSysParam = bikeSysParamService.getById("1401718233817817089");
		if(bikeSysParam==null) {
			return BikeResult.error("0","未找到对应数据",null);
		}
		return BikeResult.ok("1","成功",bikeSysParam);
	}

}
